﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Tailoring
{
    public partial class frmviewpemaish : Form
    {
        private int updatecheck;
        SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
        public frmviewpemaish()
        {
            InitializeComponent();
        }
        private string clientID = "";
        private string nkey = "";
        public string psearchkey { set { clientID = value; } }
        public string pnamekey { set { nkey = value; } get { return nkey; } }
        public string pid { set { txtid.Text = value; } get { return txtid.Text; } }
        public string pname { set { txtname.Text = value; } get { return txtname.Text; } }
        public string pmobile { set { txtmobile.Text = value; } get { return txtmobile.Text; } }
        public string pcnic { set { txtcnic.Text = value; } get { return txtcnic.Text; } }
        public string paddress { set { txtaddress.Text = value; } get { return txtaddress.Text; } }
        public string pchati
        {
            set { txtchatti.Text = value; }
        }
        public string pcity
        {
            set { cmbcity.Text = value; }
        }

        public string pdaman
        {
            set { txtdaman.Text = value; }
        }
        public string plambai
        {
            set { txtlambai.Text = value; }
        }
        public string pshalwar
        {
            set { txtshalwar.Text = value; }
        }

        public string pasteen
        {
            set { txtasteen.Text = value; }
        }
        public string ptera
        {
            set { txttera.Text = value; }
        }
        public string ppansa
        {
            set { txtpancha.Text = value; }
        }
        public string pcollor
        {
            set { txtcollar.Text = value; }
        }



        public void reccheck(SqlDataReader dr,string a)
        {
            //int countr = 0;
            //while (dr.Read())
            //{
            //    countr++;
            //}

            //if (countr > 1)
            //{
                multiclint mcfrm = new multiclint();
                mcfrm.key = nkey;
                mcfrm.ShowDialog();
               
                //mcfrm.Loadclient(a);
                this.Close();
            //}
        
        }
        public void loadclient(string a,string querystr)
        {

            
            try
            {
                conn.Open();
                string strquery = querystr;
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
                sqlcmd.Parameters.AddWithValue("@key", a);
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    int rowcount = 0;
                        while (sqldreader.Read())
                        {
                            rowcount++;
                            lbstatus.ForeColor = Color.Red;
                            lbstatus.Text = "";
                            txtid.Text = sqldreader["clientId"].ToString();
                            txtname.Text = sqldreader["name"].ToString();
                            txtaddress.Text = sqldreader["address"].ToString();
                            txtcnic.Text = sqldreader["cnic"].ToString();
                            cmbcity.Text = sqldreader["city"].ToString();
                            txtmobile.Text = sqldreader["mobile"].ToString();
                            txtlambai.Text = sqldreader["lambai"].ToString();
                            txtchatti.Text = sqldreader["chatti"].ToString();
                            txttera.Text = sqldreader["tera"].ToString();
                            txtasteen.Text = sqldreader["asteen"].ToString();
                            txtdaman.Text = sqldreader["daman"].ToString();
                            txtshalwar.Text = sqldreader["shalwar"].ToString();
                            txtpancha.Text = sqldreader["pancha"].ToString();
                            txtcollar.Text = sqldreader["collar"].ToString();
                        }
                        if (rowcount > 1)
                        {
                            reccheck(sqldreader, a);
                            //this.Close();
                        }
                    }
                
                else
                {

                    if (MessageBox.Show("No Record Found !! ",
                          "Do You Want To Try Again ?",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        Searchpopup spop = new Searchpopup();
                        spop.MdiParent = this.ParentForm;
                        spop.Show();
                        this.Close();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                sqldreader.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        public void enblecontrol()
        {
            mnedit.Enabled = false;
            txtname.ReadOnly = false;
            mnupdate.Enabled = true;
            txtaddress.ReadOnly = false;
            txtcnic.ReadOnly = false;
            cmbcity.Enabled = true;
            txtmobile.ReadOnly = false;
            lbstatus.ForeColor = Color.Red;
            lbstatus.Text = "You Can't Modify ID";
        
        }
        public void updpemysh()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //conn.Open();
                SqlCommand cmd = new SqlCommand("uppemayesh", conn);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@clientId", txtid.Text);

                //cmd.Parameters.AddWithValue("@", txtname.Text);
                cmd.Parameters.AddWithValue("@lambai", txtlambai.Text);
                cmd.Parameters.AddWithValue("@chatti", txtchatti.Text);
                cmd.Parameters.AddWithValue("@tera", txttera.Text);
                cmd.Parameters.AddWithValue("@collar", txtcollar.Text);
                cmd.Parameters.AddWithValue("@asteen", txtasteen.Text);
                cmd.Parameters.AddWithValue("@daman", txtdaman.Text);
                cmd.Parameters.AddWithValue("@shalwar", txtshalwar.Text);
                cmd.Parameters.AddWithValue("@pancha", txtpancha.Text);
                
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = cmd;
                int a=adapter.InsertCommand.ExecuteNonQuery();
                cmd.Connection.Close();
                if (a == updatecheck)
                {
                    stmessage.Text = "Update Successfull...";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                this.conn.Close();

            }
        }
        public void updclient()
        {
        try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                //conn.Open();
                SqlCommand cmd = new SqlCommand("upclient", conn);
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@clientId", txtid.Text);
                cmd.Parameters.AddWithValue("@name", txtname.Text);
                cmd.Parameters.AddWithValue("@mobile", txtmobile.Text);
                cmd.Parameters.AddWithValue("@address", txtaddress.Text);
                cmd.Parameters.AddWithValue("@cnic", txtcnic.Text);
                cmd.Parameters.AddWithValue("@city", cmbcity.Text);
                
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.InsertCommand = cmd;
                updatecheck = adapter.InsertCommand.ExecuteNonQuery();
                cmd.Connection.Close();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {
                this.conn.Close();

            }
        
        }
        
        private void vpvalidation(object sender, KeyPressEventArgs e)
        {
            {
                if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                    && e.KeyChar != '.')
                {
                    e.Handled = true;
                }
            }
        }

        private void txtid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void txtmobile_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                    && !char.IsDigit(e.KeyChar)
                && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }
        private void txtcnic_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                  && !char.IsDigit(e.KeyChar) && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }

        private void txtcnic_TextChanged(object sender, EventArgs e)
        {
            string cn = txtcnic.Text;
            if (cn.Length > 15)
            {
                toolTip1.IsBalloon = true;
                toolTip1.ToolTipIcon = ToolTipIcon.Info;
                toolTip1.ToolTipTitle = "Somthing Wrong with Data Entry";
                toolTip1.Show("Only two - and 13 Digits are Allowed Select the Text and Delete And Try Again !", txtcnic, 0, -80, 2500);
            }
        }

        private void txtid_Leave(object sender, EventArgs e)
        {
            //if (txtid.Text.Length > 0 && txtname.Text.Length > 0)
            //{
            //    lbedit.Visible = true;
            //}
            SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
            try
            {
                conn.Open();
                string strquery = "select * from tblclientinfo where clientId=" + txtid.Text;
                SqlCommand sqlcmd = new SqlCommand(strquery, conn);
                //sqlcmd.Parameters.AddWithValue("@id", txtid.Text);
                SqlDataReader sqldreader = sqlcmd.ExecuteReader();
                if (sqldreader.HasRows)
                {
                    while (sqldreader.Read())
                    {
                        txtname.Enabled = false;
                        
                        txtaddress.Enabled = false;
                        txtcnic.Enabled = false;
                        cmbcity.Enabled = false;
                        txtmobile.Enabled = false;

                        lbstatus.ForeColor = Color.Red;
                        
                    }
                }
                else
                {
                    txtname.Enabled = true;
                    //savemnpemaish.Enabled = true;
                    txtaddress.Enabled = true;
                    txtcnic.Enabled = true;
                    cmbcity.Enabled = true;
                    txtmobile.Enabled = true;
                    //lbedit.Visible = false;
                    lbstatus.ForeColor = Color.Green;
                    
                }
                sqldreader.Close();
                //if (sqldreader["clientId"] != null)
                //{
                //    lbstatus.Text = "This ID Exist Try Another One";
                //}


                //sqlcmd.Parameters.Add(new SqlParameter("@id", System.Data.DbType.Int32,10,"clientId"));
                // Fill the parameter with the value retrieved
                // from the text field
                //cmd.Parameters["@Find"].Value = txtFind.Text;

                //// Execute the query
                //rdr = cmd.ExecuteReader();

                //// Fill the list box with the values retrieved
                //lbFound.Items.Clear();
                //while (rdr.Read())
                //{
                //    lbFound.Items.Add(rdr["FirstName"].ToString() +
                //    " " + rdr["LastName"].ToString());
                //}


            }
            catch //(Exception ex)
            {
                // MessageBox.Show(ex.Message);
            }
        }

        private void frmviewpemaish_Load(object sender, EventArgs e)
        {
            //Searchpopup spop = new Searchpopup();
            //spop.MdiParent = this.ParentForm;
            //spop.Show();
            txtid.ReadOnly = true;
            txtname.ReadOnly = true;
            
            txtaddress.ReadOnly = true;
            txtcnic.ReadOnly = true;
            cmbcity.Enabled = false;
            txtmobile.ReadOnly = true;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ((txtid.Text.Length > 0) || (txtname.Text.Length > 0) || (txtmobile.Text.Length > 0))
            {
                if (MessageBox.Show("Are you Sure to Close the Form?",
                          "View Pemaish Application Closing Confirmation",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    this.Close();
                }

            }
            else
            {
                this.Close();
            }
        }

        private void editToolStripMenuItem2_Click(object sender, EventArgs e)
        {
           
            enblecontrol();
        }

        private void mnupdate_Click(object sender, EventArgs e)
        {
            updclient();
            updpemysh(); 
}

        private void searchAgainToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Searchpopup spop = new Searchpopup();
           
            spop.ShowDialog();
            spop.TopMost = true;
        }

        private void mainFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mdistrip strip = new mdistrip();
            strip.MdiParent = this.ParentForm;

            strip.Show();
            this.Close();
        }

        private void frmviewpemaish_FormClosed(object sender, FormClosedEventArgs e)
        {
            //mdistrip strip = new mdistrip();
            //strip.MdiParent = this.ParentForm;

            //strip.Show();
            
        }
        private void txtlambai_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal lambai = Convert.ToDecimal(txtlambai.Text);
                if (lambai < 13 || lambai > 50)
                {

                    e.Cancel = true;
                    txtlambai.Select(0, txtlambai.Text.Length);


                    this.errorProvider1.SetError(txtlambai, "Values Limit Exceded Specify value Between 13 And 50");

                }
            }
            catch { }
        }

        private void txtlambai_Validated(object sender, EventArgs e)
        {

            errorProvider1.SetError(txtlambai, "");
        }

        private void txtchatti_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal chatti = Convert.ToInt32(txtchatti.Text);
                if (chatti < 18 || chatti > 45)
                {

                    e.Cancel = true;
                    txtchatti.Select(0, txtchatti.Text.Length);


                    this.errorProvider1.SetError(txtchatti, "Values Limit Exceded Specify value Between 18 And 45");

                }
            }
            catch  { }
        }

        private void txtchatti_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtchatti, "");
        }

        private void txtcollar_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtcollar, "");
        }

        private void txtcollar_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal collar = Convert.ToInt32(txtcollar.Text);
                if (collar < 9 || collar > 25)
                {

                    e.Cancel = true;
                    txtcollar.Select(0, txtcollar.Text.Length);


                    this.errorProvider1.SetError(txtcollar, "Values Limit Exceded Specify value between 9 and 25");

                }
            }
            catch { }
        }

        private void txtasteen_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal asteen = Convert.ToInt32(txtasteen.Text);
                if (asteen < 8 || asteen > 28)
                {

                    e.Cancel = true;
                    txtasteen.Select(0, txtasteen.Text.Length);


                    this.errorProvider1.SetError(txtasteen, "Values Limit Exceded Specify value between 8 and 25");

                }
            }
            catch { }
        }

        private void txtasteen_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtasteen, "");
        }

        private void txttera_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal tera = Convert.ToInt32(txttera.Text);
                if (tera < 8 || tera > 23)
                {

                    e.Cancel = true;
                    txttera.Select(0, txttera.Text.Length);


                    this.errorProvider1.SetError(txttera, "Values Limit Exceded Specify value between 8 and 23");

                }
            }
            catch { }
        }

        private void txttera_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txttera, "");
        }

        private void txtdaman_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal daman = Convert.ToInt32(txtdaman.Text);
                if (daman < 10 || daman > 35)
                {

                    e.Cancel = true;
                    txtdaman.Select(0, txtdaman.Text.Length);


                    this.errorProvider1.SetError(txtdaman, "Values Limit Exceded Specify value between 10 and 35");


                }
            }
            catch { }

        }

        private void txtdaman_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtdaman, "");
        }

        private void txtpancha_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal pancha = Convert.ToInt32(txtpancha.Text);
                if (pancha < 7 || pancha > 15)
                {

                    e.Cancel = true;
                    txtpancha.Select(0, txtpancha.Text.Length);


                    this.errorProvider1.SetError(txtpancha, "Values Limit Exceded Specify value between 7 and 15");

                }
            }
            catch { }
        }

        private void txtpancha_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtpancha, "");
        }

        private void txtshalwar_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                decimal shalwar = Convert.ToInt32(txtshalwar.Text);

                if (shalwar < 10 || shalwar > 45)
                {

                    e.Cancel = true;
                    txtshalwar.Select(0, txtshalwar.Text.Length);


                    this.errorProvider1.SetError(txtshalwar, "Values Limit Exceded Specify value between 10 and 45");

                }
            }
            catch { }
        }

        private void txtshalwar_Validated(object sender, EventArgs e)
        {
            this.errorProvider1.SetError(txtshalwar, "");
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmrptpemaish pview = new frmrptpemaish();
            pview.Keyid = txtid.Text;
            pview.ShowDialog();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {if (MessageBox.Show("Are you Sure to Delete this Record?",
                          "Be Care This is Cannot Be Undo",
                           MessageBoxButtons.YesNo,
                           MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    try
            {
                conn.Open();
                string strdelpemaish = "Delete from tblpemaish where tblpemaish.clientId=" + txtid.Text;
                SqlCommand sqlcmd = new SqlCommand(strdelpemaish, conn);
                sqlcmd.CommandType = CommandType.Text;
                int a=sqlcmd.ExecuteNonQuery();
                string strdelorder = "Delete from tblorder where tblorder.clientId=" + txtid.Text;
                SqlCommand sqlcmd3 = new SqlCommand(strdelorder, conn);
                sqlcmd3.CommandType = CommandType.Text;
                int c = sqlcmd3.ExecuteNonQuery();
                string strdelclient = "Delete from tblclientinfo where clientId=" + txtid.Text;
                SqlCommand sqlcmd2 = new SqlCommand(strdelclient, conn);
                sqlcmd2.CommandType = CommandType.Text;
                int b=sqlcmd2.ExecuteNonQuery();
                if (a > 0 && a == b)
                {
                    MessageBox.Show("Record Deleted Successfully !", "Deletion Of Client", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                            conn.Close();

            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.Message); 
            }
        }
                }

            
        
        }
 }